package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.logiarea.Logiarea;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.logiarea.ILogiareaService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物流运作区域信息Controller
 * Created by weih on 2016-10-14.
 */
@Controller
@RequestMapping("/mdm/logiarea")
public class LogiareaController extends BaseController {

    @Autowired
    ILogiareaService logiareaService;
    /**
     * 跳转新增物流运作区域页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/logiarea/add";
    }
    /**
     * 新增物流运作区域信息
     * @param logiarea 物流运作区域对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addLogiarea(@RequestBody Logiarea logiarea,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logiarea.setCreateUser(getSessionUser().getUserId());
        logiarea.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logiareaService.addLogiarea(logiarea);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑物流运作区域页面
     * @param logiareaCode  物流运作区域代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String logiareaCode,Model model){
        model.addAttribute("logiarea", logiareaService.getLogiareaByCode(logiareaCode));
        return "mdm/logiarea/edit";
    }

    /**
     * 编辑物流运作区域信息
     * @param logiarea
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editLogiarea(@RequestBody Logiarea logiarea,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Logiarea oldLogiarea=logiareaService.getLogiareaByCode(logiarea.getLogiareaCode());
        oldLogiarea.setId(logiarea.getId());
        oldLogiarea.setLogisticsAreaCode(logiarea.getLogisticsAreaCode());
        oldLogiarea.setLogisticsAreaName(logiarea.getLogisticsAreaName());
        oldLogiarea.setOrgType(logiarea.getOrgType());
        oldLogiarea.setCompanyCode(logiarea.getCompanyCode());
        oldLogiarea.setDealerCode(logiarea.getDealerCode());
        oldLogiarea.setCountryCode(logiarea.getCountryCode());
        oldLogiarea.setLatitude(logiarea.getLatitude());
        oldLogiarea.setLongitude(logiarea.getLongitude());
        oldLogiarea.setUpdateUser(getSessionUser().getUserId());
        oldLogiarea.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logiareaService.updateLogiarea(oldLogiarea);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除物流运作区域信息
     * @param logiarea 物流运作区域对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteLogiarea(@RequestBody Logiarea logiarea,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logiarea.setUpdateUser(getSessionUser().getUserId());
        logiarea.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logiareaService.deleteLogiarea(logiarea);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

	/**
     * 跳转物流运作区域明细页面
     * @param logiareaCode  大区代码
     */
    @RequestMapping("/detail")
    public String toDetailPage(String logiareaCode,Model model){
        Logiarea logiarea = logiareaService.getLogiareaByCode(logiareaCode);
        model.addAttribute("logiarea", logiarea);

        model.addAttribute("nation", regionService.getRegionByCode(logiarea.getNationCode()));
        return "mdm/logiarea/detail";
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/logiarea/list";
    }

    /**
     * 分页查询数据
     * @param logiarea 物流运作区域对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Logiarea> getLogiareaData(Logiarea logiarea ,DataTableRequest dtrequest){
        int cnt = (int)logiareaService.getLogiareaCount(logiarea);
        List<Logiarea> list = logiareaService.queryLogiareas(logiarea, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Logiarea> result = new PaginationResult<Logiarea>(list, pi);
        return new DataTableResponse<Logiarea>(dtrequest.getsEcho(),result);
    }

}
